//
//  BillViewController.m
//  Karaoke
//
//  Created by 蒋 浩锋 on 12-8-14.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "BillViewController.h"
#import "MenuObject.h"
#import "BillTableViewCell.h"
#import "OKFolioObject.h"
#import "DataCenter.h"
#import "ServerNetworkCenter.h"
#import "ClientNetworkCenter.h"
#import "FXLabel.h"
#import "SocketDataHandle.h"
#import "RoomInfoBarView.h"


@interface BillViewController ()
{
    NSMutableArray *selectedFoods;
    
    __weak IBOutlet FXLabel *lblReserveClerk;
    
    __weak IBOutlet FXLabel *lblOverLowLimit;
    __weak IBOutlet FXLabel *lblLowLimitMoney;
    
    __weak IBOutlet UITableView *tvMain;
    __weak IBOutlet FXLabel *lblCheckInTime;
    
    __weak IBOutlet FXLabel *lblZheHouJinE;
    __weak IBOutlet FXLabel *lblFoodMoney;
    __weak IBOutlet UIImageView *syLogo;
    
    __weak IBOutlet UIImageView *sdLogo;
    
}

@end

@implementation BillViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    RoomInfoBarView *roomInfobar = [[RoomInfoBarView alloc] initWithXib];
    roomInfobar.frame = CGRectMake(10.0f, 19, roomInfobar.frame.size.width, roomInfobar.frame.size.height);
    [self.view addSubview:roomInfobar];
    
    [[NSNotificationCenter defaultCenter]   addObserver :self
                                            selector    :@selector(didReceiveDataFromSocket:)
                                            name        :@"getDataFromCenter" object:nil];
    [[ServerNetworkCenter sharedInstance] sentMsg:rtBillInfo:[DataCenter sharedInstance].roomNo, @"0", @"", @"0",[DataCenter sharedInstance].tempMasterID,@"0", nil];
	// Do any additional setup after loading the view.
    if (OEM_FLAG == 1) {
        syLogo.hidden = YES;
        sdLogo.hidden = NO;
    }
}

- (void)viewDidUnload
{
    lblReserveClerk = nil;
    lblLowLimitMoney = nil;
    lblOverLowLimit = nil;
    lblCheckInTime = nil;
    lblFoodMoney = nil;
    lblZheHouJinE = nil;
    tvMain = nil;
    syLogo = nil;
    sdLogo = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
- (IBAction)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UITableView Data Source.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return selectedFoods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BillTableViewCell";
    
    BillTableViewCell *cell = (BillTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [nibs objectAtIndex:0];
    }
    
    OKFolioObject *obj = [selectedFoods objectAtIndex:indexPath.row];
    
    if ([obj.IfPackChild isEqualToString:@"1"]) {
        cell.lblName.text = [NSString stringWithFormat:@"    %@", obj.FoodName];
//        cell.imgcellBg.image = [UIImage imageNamed:@"bill1.png"];
    } else {cell.lblName.text = obj.FoodName; }
    
    if ([obj.Price floatValue] == 0) {
        cell.lblPrice.text = [NSString stringWithFormat:@"%d %@", obj.Quantity, obj.Monad];
    } else {
        cell.lblPrice.text = [NSString stringWithFormat:@"%@ * %d %@", obj.Price, obj.Quantity, obj.Monad];
    }
    
    NSMutableString *note = [[NSMutableString alloc] init];
    
    if ([obj.Note isEqualToString:@"1"]) {
        [note appendString:@"退"];
    }
    
    if ([obj.IfPackChild isEqualToString:@"1"]) {
        if (note.length > 0) {
            [note appendString:@"--"];
        } else {
            [note appendString:@"套餐内项目"];
        }
    }
    
    if ([obj.Flag isEqualToString:@"F"]) {
        if (obj.GroupID.length == 0) {
            if (note.length > 0) {
                [note appendString:@"--赠送"];
            } else {
                [note deleteCharactersInRange:NSMakeRange(0, note.length)];
                [note appendString:@"赠送"];
            }
        } else {
            if (note.length > 0) {
                [note appendFormat:@"%@%@", @"--赠送--", obj.GroupID];
            } else {
                [note deleteCharactersInRange:NSMakeRange(0, note.length)];
                [note appendFormat:@"%@%@", @"赠送--", obj.GroupID];
            }
        }
    }
    
    cell.lblFlag.text = note;
    
    if ([obj.Price floatValue] == 0) {
        cell.lblAmount.text = @"";
    } else {
        cell.lblAmount.text = [NSString stringWithFormat:@"%8.1f", [obj.Price floatValue] * obj.Quantity];
    }
    cell.lblLuoDanRwn.text = obj.InputClerk;
    cell.lblDate.text = obj.InputTime;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

- (void)didReceiveDataFromSocket:(NSNotification *)notification
{
    NSString    *tmpCommandCode = [[notification userInfo] objectForKey:@"code"];
    NSString    *tmpCommandDetail = [[notification userInfo] objectForKey:@"detail"];
    
    if (![tmpCommandCode isEqualToString:@"tcpMsg"]) {
        return;
    }
    
    NSLog(@"inputAmountView  received msg: %@", tmpCommandDetail);
    
    NSArray *tmpArray = [tmpCommandDetail componentsSeparatedByString:@"\r\n"];
    int     cmd = [[tmpArray objectAtIndex:1] intValue];
    switch (cmd) {
        case rtBillInfo:
            
            if ([[tmpArray objectAtIndex:2] isEqualToString:@"1"]) {
                UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                            message             :[tmpArray objectAtIndex:3]
                                                            delegate            :self
                                                            cancelButtonTitle   :@"确定"
                                                            otherButtonTitles   :nil, nil];
                [alert show];
                return;
            }
            
            lblReserveClerk.text = [NSString stringWithFormat:@"%@ / %@", [tmpArray objectAtIndex:4], [tmpArray objectAtIndex:5]];
            lblCheckInTime.text = [tmpArray objectAtIndex:6];
            lblLowLimitMoney.text = [tmpArray objectAtIndex:9];
            lblFoodMoney.text = [tmpArray objectAtIndex:10];
            lblZheHouJinE.text = [NSString stringWithFormat:@"%8.2f", [[tmpArray objectAtIndex:11] floatValue] + [[tmpArray objectAtIndex:12] floatValue]];
            lblOverLowLimit.text = [NSString stringWithFormat:@"%8.2f", [[tmpArray objectAtIndex:12] floatValue] - [[tmpArray objectAtIndex:9] floatValue]];
            selectedFoods = [[NSMutableArray alloc]init];
            for (int i = 14; i < tmpArray.count - 1; i += 11) {
                OKFolioObject *obj = [[OKFolioObject alloc] init];
                obj.FoodName = [tmpArray objectAtIndex:i];
                obj.Price = [tmpArray objectAtIndex:i + 1];
                obj.Quantity = [[tmpArray objectAtIndex:i + 2] intValue];
                obj.Monad = [tmpArray objectAtIndex:i + 3];
                obj.Flag = [tmpArray objectAtIndex:i + 4];
                obj.LimitDisRate = [tmpArray objectAtIndex:i + 5];
                obj.IfPackChild = [tmpArray objectAtIndex:i + 6];
                obj.Note = [tmpArray objectAtIndex:i + 7];
                obj.InputClerk     = [tmpArray objectAtIndex:i+8];
                obj.InputTime      = [tmpArray objectAtIndex:i+9];
                obj.GroupID = [tmpArray objectAtIndex:i + 10];
                [selectedFoods addObject:obj];
            }
            
            [tvMain reloadData];
            break;
    }
}

@end
